package com.threeti.mecool.core.domain.model.acl;

import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;
import com.threeti.mecool.framework.util.Sets;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.Collections;
import java.util.Set;

/**
 * (明酷督导管理系统)用户
 * <p/>
 * 用户已经内含ROLE_USER权限，在新建用户时无需显式指定
 *
 * @author Jay Meng
 */
@Entity
@Table(name = "DM_USER",
        uniqueConstraints = @UniqueConstraint(columnNames = {"EMP_PK"}))
public class User extends DefaultDomainEntity {

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "DM_USER_SYSROLE", joinColumns = {@JoinColumn(name = "USER_ID")}, inverseJoinColumns = {@JoinColumn(name = "SYSROLE_ID")})
    @Fetch(FetchMode.SUBSELECT)
    private Set<SystemRole> systemRoles = Sets.newHashSet();//just to prevent duplicate system role, by using Set,it is not necessary to specify combined unique constraint

    @ManyToOne(optional = false)
    private PermissionRole permissionRole;

    @Embedded
    private OrgnizationEmployee employee;

    @Column(nullable = false, unique = true)
    private String loginName;

    private String displayName;

    @Column(nullable = false)
    private String password;

    private String imei;

    private Integer disableImeiValidate;

    @Enumerated(EnumType.ORDINAL)
    private UserStatus status = UserStatus.INACTIVE;

    public User(String loginName, String password) {
        this.loginName = loginName;
        this.password = password;
        this.status = UserStatus.ACTIVE;//默认激活
    }

    public User(String loginName, String password, SystemRole systemRole) {
        this(loginName, password, null, systemRole);
    }

    public User(String loginName, String password, String displayName) {
        this(loginName, password);
        this.displayName = displayName;
    }

    public User(String loginName, String password, String displayName, SystemRole newSystemRole) {
        this(loginName, password, displayName);
        Validate.isTrue(newSystemRole != null && StringUtils.isNotBlank(newSystemRole.getName()), "用户系统角色不能为空");
        Validate.isTrue(newSystemRole.getId() != null && newSystemRole.getId() > 0, "无法新建用户系统角色，系统角色在系统中必须已经初始存在");
        systemRoles.add(newSystemRole);
    }

    /**
     * 添加系统角色
     */
    public void addSystemRole(SystemRole systemRole) {
        systemRoles.add(systemRole);
    }

    /**
     * 绑定自定义角色(该角色已绑定了特定的操作集合)
     */
    public User bindWithCustomizedPermissionRole(PermissionRole permRole) {
        permissionRole = permRole;
        return this;
    }

    /**
     * 用户的系统角色集
     */
    public Set<SystemRole> getSystemRoles() {
        return Collections.unmodifiableSet(systemRoles);
    }

    /**
     * 取得被分配的自定义角色
     */
    public PermissionRole getPermissionRole() {
        return permissionRole;
    }

    /**
     * 用户对应的(组织机构)员工
     */
    public OrgnizationEmployee getEmployee() {
        return employee;
    }

    public String getLoginName() {
        return loginName;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getPassword() {
        return password;
    }

    public UserStatus getStatus() {
        return status;
    }

    public String getImei() {
        if (imei == null) {
            imei = "";
        }
        return imei;
    }

    public Integer getDisableImeiValidate() {
        if (disableImeiValidate == null) {
            disableImeiValidate = 0;
        }
        return disableImeiValidate;
    }

    @Override
    public String toString() {
        return "User [employee=" + employee + ", loginName=" + loginName + ", displayName=" + displayName + ", status=" + status + "]";
    }

    public User() {
        super();
        this.setStatus(UserStatus.ACTIVE);//默认激活
    }

    public User(Long id) {
        this.setId(id);
    }

    public void setEmployee(OrgnizationEmployee employee) {
        this.employee = employee;
    }

    public void setSystemRoles(Set<SystemRole> systemRoles) {
        this.systemRoles = systemRoles;
    }

    public void setPermissionRole(PermissionRole permissionRole) {
        this.permissionRole = permissionRole;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setStatus(UserStatus status) {
        this.status = status;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public void setDisableImeiValidate(Integer disableImeiValidate) {
        this.disableImeiValidate = disableImeiValidate;
    }
}
